package com.lmabbe.common.global.context.session;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.ParserConfig;
import com.lmabbe.common.global.constant.GlobalConstant;
import com.lmabbe.common.global.context.LmabbeSessionUser;
import com.lmabbe.common.global.enums.ClientTypeEnum;
import org.apache.dubbo.rpc.RpcContext;

/**
 * Rpc端实现session上下文
 *
 * @author liming
 * @return {@link null}
 * @date 2021/12/7 5:05 下午
 */
public class RpcLmabbeSessionUserImpl implements LmabbeSessionUser {

    @Override
    public Long getTenantId() {
        return Long.parseLong(RpcContext.getContext().getAttachment(GlobalConstant.SYSTEM.TENANT_ID));
    }

    @Override
    public String token() {
        return RpcContext.getContext().getAttachment(GlobalConstant.SYSTEM.AUTHORIZATION)
                .replace(GlobalConstant.SYSTEM.AUTHENTICATION_PREFIX, "");
    }

    @Override
    public ClientTypeEnum clientType() {
        return ClientTypeEnum.getClientTypeByCode(RpcContext.getContext().getAttachment(GlobalConstant.SYSTEM.FROM_CLIENT));
    }

    @Override
    public LoginUser loginUser() {
        ParserConfig.getGlobalInstance().setAsmEnable(false);
        return JSONObject.parseObject(RpcContext.getContext().getAttachment(GlobalConstant.SYSTEM.LOGIN_USER), LoginUser.class);
    }
}
